<?php

class importDataTask extends sfBaseTask {

    protected function configure() {
        // // add your own arguments here
        // $this->addArguments(array(
        //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
        // ));

        $this->addOptions(array(
            new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend'),
            new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
            new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
            new sfCommandOption('import-file', null, sfCommandOption::PARAMETER_REQUIRED, 'The name of the file to import', 'export.xml'),
            new sfCommandOption('resource-path', null, sfCommandOption::PARAMETER_REQUIRED, 'The relative directory where resource files are located', 'resources'),
            new sfCommandOption('ingest-path', null, sfCommandOption::PARAMETER_REQUIRED, 'The located to put resource to have them ingested'),
                // add your own options here
        ));

        $this->namespace = 'travide';
        $this->name = 'import-data';
        $this->briefDescription = 'brief description';
        $this->detailedDescription = <<<EOF
The [importData|INFO] task does things.
Call it with:

  [php symfony importData|INFO]
EOF;
    }

    protected function execute($arguments = array(), $options = array()) {

        // initialize the database connection
        $databaseManager = new sfDatabaseManager($this->configuration);
        $connection = $databaseManager->getDatabase($options['connection'])->getConnection();

        if (file_exists($options['import-file'])) {
            $this->logSection('init', 'Using import file: ' . $options['import-file']);
            $this->logSection('import', 'Starting');
            $dataImporter = new DataImporterTravide();
            $dataImporter->setResourceBasePath($options['resource-path']);
            $dataImporter->setResourceIngestPath($options['ingest-path']);
            $dataImporter->importData($options['import-file']);
            $this->logSection('import', 'Done');
        } else {
            $this->logSection('error', 'Import file: ' . $options['import-file'] . ' not found');
        }

        // add your code here
        /**
         * This is currently a test task for the data importer task
         */
    }

}
